package com.hmall.order.listener;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmall.common.client.ItemClient;
import com.hmall.order.dto.RabbitemplateOrder;
import com.hmall.order.mapper.OrderMapper;
import com.hmall.order.pojo.DownOrder;
import com.hmall.order.pojo.Order;
import com.hmall.order.service.IOrderService;
import io.seata.spring.annotation.GlobalTransactional;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 监听失效订单
 */
@Component
public class OrderListener extends ServiceImpl<OrderMapper, Order> {

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private ItemClient itemClient;


    @RabbitListener(queues = "dl.queue")
    @GlobalTransactional
    public void handleOrderMsg(RabbitemplateOrder rabbitemplateOrder){
        Order order = orderMapper.selectById(rabbitemplateOrder.getId());
        if (order.getStatus()==1){
            order.setStatus(5);
            orderMapper.updateById(order);
            //恢复库存
            itemClient.deductItemStock(rabbitemplateOrder.getItemId(),rabbitemplateOrder.getNum());
        }
    }

}